<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('member')->checklogin();
set_html('title','订单详情');
if($_RQ['op']=='main'){
	//返回链接
	$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
	$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$shopset=$this->plugin_setting('shopset');
	if(!$isfollow && $_RQ['fromcreate'] && !$shopset['noshowqrcode']){
		$return['showqrcode']=1;
	}
	if($_RQ['id']){
		$id = intval($_RQ['id']);
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
	}elseif($_RQ['ordersn']){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>trim($_RQ['ordersn'])));
	}
	if(!$order){
		exi('指定订单不存在！','error','goback');
	}
	if($order['status']==0){
		$order['fromseconds']=time()-$order['createtime'];
		$order['outseconds']=$shopset['orderpay_outtime']*3600;
		if($order['fromseconds']<$order['outseconds']){
			$order['fromseconds']=($order['fromseconds']/$order['outseconds'])*428;
			$order['upseconds']=$this->model()->date($order['outseconds']-$order['fromseconds']);
		}else{
			$order['upseconds']=0;
		}
	}
	//定义订单状态名称
	if($order['status']==0 && $order['paytype']!=3){
		$order['statustitle']='订单待支付';
		$order['statusdesc']='超时未支付，订单将自动取消';
	}elseif($order['status']==0 && $order['paytype']==3){
		$order['statustitle']='货到付款';
		$order['statusdesc']='请您收到商品后付款';
	}elseif($order['status']==1 && !$order['refundstate']){
		$order['statustitle']='买家已付款';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==2 && !$order['refundstate']){
		$order['statustitle']='商家已发货';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==3 && !$order['refundstate']){
		$order['statustitle']='订单已完成';
		$order['statusdesc']='';
	}elseif($order['status']==4 && !$order['refundstate']){
		$order['statustitle']='订单已关闭';
		$order['statusdesc']='';
	}elseif($order['refundstate']>0 && $order['status']==1){
		$order['statustitle']='申请退款中';
		$order['statusdesc']='请您留意退款进度';
	}elseif($order['refundstate']>0 && $order['status']>1){
		$order['statustitle']='申请售后中';
		$order['statusdesc']='请您留意售后进度';
	}
	//包含核销商品
	if($order['status']==2 && $order['isverify']==1){
		$order['statustitle']='部分商品已发货';
		$order['statusdesc']='除核销商品，其他商品已发货';
	}elseif($order['status']==2 && $order['isverify']==2){
		$order['statustitle']='等待核销';
		$order['statusdesc']='属核销商品，请核销！';
	}elseif($order['status']==3 && $order['isverify']==2){
		$order['statustitle']='已核销';
		$order['statusdesc']='属核销商品，已核销完成！';
	}
	$order['express']=$this->pdo_get('order_express',array('orderid'=>$order['id']));
	$order['express']['express_name']=$this->express_types[$order['express']['express_id']]['name'];
	//时间格式化
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	$order['paytime']=date('Y-m-d H:i:s',$order['paytime']);
	$order['sendtime']=$order['express']['createtime']?date('Y-m-d H:i:s',$order['express']['createtime']):'';
	$order['finishtime']=date('Y-m-d H:i:s',$order['finishtime']);
	if ($order['userdeleted'] == 2) {
			exi('订单已经被删除!','error');
	}
	//用户地址
	if($order['addressid']){
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));                       //用户信息
    $ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
	$merchs =array();
	foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','type','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$gv['title']=$goodsinfo['title'];
			$gv['image']=$goodsinfo['image'];
			$gv['merchid']=$goodsinfo['merchid'];
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$goodsinfo['buytypetitle']=$this->buytypes()[$order['buytype']];
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
			if($order['storeid']){
				$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$order['storeid']));
			}
	}
	$return['merchs']=$merchs;
	$return['order_gifts']=$this->pdo_get('order_gift',array('order_id'=>$order['id']));
	$return['order_gifts']['gifts']=json_decode($return['order_gifts']['gifts'],true);
	$stores_count=count($stores);
	if (!empty($order['diyformfields']) && !empty($order['diyformdata'])) {
		$order_fields = $order['diyformfields'];
		$order_data = $order['diyformdata'];
	}
	
	$order['can_refund']=$this->model('order/refund')->check_order_rerefund($order['id']);
	$return['stores_count']=$stores_count;
	if($order['sendtype']==3 && $order['status']==3){
		$order['content']=$this->pdo_getcolumn('order_virtual',array('orderid'=>$order['id']),'content');
	}
	$order['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),'total');
	$return['order']=$order;
	$return['address']=$address;
	$return['member']=$member;
	$return['goods']=$ordergoods;
	$return['stores']=$stores;
	$return['carrier']=$carrier;
	$return['order_fields']=$order_fields;
	$return['order_data']=$order_data;
	$order_verify=$this->pdo_getall('order_verify',array('orderid'=>$order['id']));
	if($order_verify){
			foreach($order_verify as &$verify){
				$verify['verifycode']=$verify['verify_code'];
				if($verify['status']==0 && $verify['endtime'] && $verify['endtime']<TIMESTAMP){
					$verify['status']=2;
					$this->pdo_update('order_verify',array('status'=>2),array('id'=>$verify['id']));
				}else{
					$verify['url']=custom_url('jy_weishop/verify.detail//id='.$order['id'].'&verifycode='.$verify['verify_code'],true);
					//小程序二维码：
					$url='/jy_weishop/verify/detail/index?id='.$order['id'].'&verifycode='.$verify['verify_code'];
					$pid='verifyid'.$order['id'].'c'.$verify['verify_code'];
					$verify['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
				}
			}
	}
	$return['order_verify']=$order_verify;
	$goods_verify=$this->pdo_getall('verifygoods',array('orderid'=>$order['id']));
	if($goods_verify){
			foreach($goods_verify as &$gvv){
				if (!(empty($gvv['limitnum']))){
					$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id']));
					$verifynum = 0;
					foreach ($verifygoodlogs as $verifygoodlog ) 
					{
						$verifynum += intval($verifygoodlog['verifynum']);
					}
					$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
					if (empty($gvv['limittype'])) {
						$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
					}else{
						$limitdate = intval($gvv['limitdate']);
					}
					if ($limitdate < time()) {
						$gvv['status']=3;
					}
				}
				$gvv['url']=custom_url('jy_weishop/verify.verifygoodsdetail//id='.$order['id'].'&verifycode='.$gvv['verifycode'],true);
				//小程序二维码：
				$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
				$pid='verifygoodsid'.$order['id'].'c'.$verify['verify_code'];
				$gvv['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
			}
	}
	$return['goods_verify']=$goods_verify;
	exi($return);
}
//删除订单
if($_RQ['op']=='delete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_delete($_RQ['id'],true);
	exi('已删除订单!');
}
//恢复订单
if($_RQ['op']=='reset'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_reset($_RQ['id'],true);
	exi('已恢复订单!');
}
//彻底删除订单
if($_RQ['op']=='rdelete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_rdelete($_RQ['id']);
	exi('已彻底删除订单!');
}
//取消订单
if($_RQ['op']=='cancel'){
	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_cancel($id);
		exi('取消成功！','','order.list/');		
	}else{		
		exi('取消失败！','error');
	}
}
//确认订单
if($_RQ['op']=='confirm'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$update=array('status'=>'3');
	pdo_update('jy_weishop_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('已确认收货!');
}
//支付订单
if($_RQ['op']=='setpay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
?>